package com.example.kehoobackend.controller;


import com.example.kehoobackend.common.CommonResponse;
import com.example.kehoobackend.domain.Notice;
import com.example.kehoobackend.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@CrossOrigin
@RestController
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 选取用户的某一类型消息
     *
     * @param user_id 用户id
     * @param type 消息类型
     * @return CommonResponse<List < Notice>>
     */
    @GetMapping("/notice")
    public CommonResponse<List<Notice>> getAllNotice(
            @RequestParam("user_id") Integer user_id,
            @RequestParam(value = "type", required = false) Integer type) {
        return CommonResponse.createForSuccess("消息列表", noticeService.getAllNotice(user_id, type));
    }



    /**
     * 发送消息
     *
     * @param notice 消息
     * @return CommonResponse<Boolean>
     */
    @PostMapping("/notice")
    public CommonResponse<Boolean> sendNotice(
            @RequestBody Notice notice) {
        return noticeService.sendNotice(notice)
                ? CommonResponse.createForSuccess("发送成功", true)
                : CommonResponse.createForSuccess("发送失败", false);
    }


    /**
     * 删除消息
     *
     * @param notice_id 消息id
     * @return CommonResponse<Boolean>
     */
    @DeleteMapping("/notice")
    public CommonResponse<Boolean> deleteNotice(
            @RequestParam("notice_id") Integer notice_id) {
        return noticeService.deleteNotice(notice_id)
                ? CommonResponse.createForSuccess("删除成功", true)
                : CommonResponse.createForSuccess("删除失败", false);
    }


}
